<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Tumblr Service Driver Library
 * http://www.tumblr.com/api
 *
 * $Id$
 *
 * @package		Service Module
 * @author		Alex Sancho
 * @copyright	(c) 2008 Alex Sancho
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *	  contributors may be used to endorse or promote products derived
 *	  from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

class Service_Tumblr_Driver extends Service_Driver
{
	protected $api_url = ".tumblr.com/api";
	protected $api_id;
	protected $email;
	protected $password;

	/**
	 *	__construct
	 *
	 * @param array $config
	 * @return void
	 * @access public
	 *
	 */
	public function __construct($config = array())
	{
		$config += Kohana::config('service.tumblr');

		foreach($config as $key => $val)
			$this->$key = $val;
	}

	/**
	 * read
	 *
	 * @param integer $start
	 * @param integer $num
	 * @param string $type
	 * @param integer $id
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function read($start = '0', $num = '50', $type = '', $id = null)
	{
		if ($id !== null)
		{
			$api_url = '/read/'.$id;
		}
		else
		{
			$params = array
			(
				'start' => $start,
				'num'	=> $num,
				'type'	=> $type
			);

			$api_url = '/read/?'.http_build_query($params);
		}

		return $this->api_call($api_url);
	}

	/**
	 * write_post
	 *
	 * @param string $title
	 * @param string $body
	 * @param boolean $private
	 * @param string $tags
	 * @param string $format
	 * @param integer $group
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function write_post($title, $body, $private = '', $tags = '', $format = '', $group = '')
	{
		$params = array
		(
			'type'	=> 'regular',
			'title' => $title,
			'body'	=> $body
		);

		$params += $this->extra($private, $tags, $format, $group);

		return $this->post($params)->api_call('/write');
	}

	/**
	 * write_photo
	 *
	 * @param string $source
	 * @param string $data
	 * @param string $caption
	 * @param string $linked
	 * @param string $private
	 * @param string $tags
	 * @param string $format
	 * @param string $group
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function write_photo($source = '', $data = '', $caption = '', $linked = '', $private = '', $tags = '', $format = '', $group = '')
	{
		if ($source == '' and $data == '')
			throw new Kohana_User_Exception('Tumblr Driver', Kohana::lang('service.photo_error'));

		$params = array
		(
			'type'				=> 'photo',
			'source'			=> $source,
			'data'				=> $data,
			'caption'			=> $caption,
			'click-through-url' => (bool) $linked
		);

		$params += $this->extra($private, $tags, $format, $group);

		return $this->post($params)->api_call('/write');
	}

	/**
	 * write_quote
	 *
	 * @param string $quote
	 * @param string $source
	 * @param string $private
	 * @param string $tags
	 * @param string $format
	 * @param string $group
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function write_quote($quote, $source = '', $private = '', $tags = '', $format = '', $group = '')
	{
		$params = array
		(
			'type'		=> 'quote',
			'quote' 	=> $quote,
			'source'	=> $source
		);

		$params += $this->extra($private, $tags, $format, $group);

		return $this->post($params)->api_call('/write');
	}

	/**
	 * write_link
	 *
	 * @param string $url
	 * @param string $name
	 * @param string $description
	 * @param string $private
	 * @param string $private
	 * @param string $tags
	 * @param string $format
	 * @param string $group
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function write_link($url, $name = '', $description = '', $private = '', $tags = '', $format = '', $group = '')
	{
		$params = array
		(
			'type'			=> 'link',
			'url'			=> $url,
			'name'			=> $name,
			'description'	=> $description
		);

		$params += $this->extra($private, $tags, $format, $group);

		return $this->post($params)->api_call('/write');
	}

	/**
	 * write_conversation
	 *
	 * @param string $conversation
	 * @param string $title
	 * @param string $private
	 * @param string $tags
	 * @param string $format
	 * @param string $group
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function write_conversation($conversation, $title = '', $private = '', $tags = '', $format = '', $group = '')
	{
		$params = array
		(
			'type'			=> 'conversation',
			'conversation'	=> $conversation,
			'title'			=> $title
		);

		$params += $this->extra($private, $tags, $format, $group);

		return $this->post($params)->api_call('/write');
	}

	/**
	 * write_video
	 *
	 * @param string $embed
	 * @param string $data
	 * @param string $title
	 * @param string $caption
	 * @param string $private
	 * @param string $tags
	 * @param string $format
	 * @param string $group
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function write_video($embed = '', $data = '', $title = '', $caption = '', $private = '', $tags = '', $format = '', $group = '')
	{
		if ($embed == '' and $data == '')
			throw new Kohana_User_Exception('Tumblr Driver', Kohana::lang('service.video_error'));

		$params = array
		(
			'type'		=> 'video',
			'embed'		=> $embed,
			'data'		=> $data,
			'title' 	=> $title,
			'caption'	=> $caption
		);

		$params += $this->extra($private, $tags, $format, $group);

		return $this->post($params)->api_call('/write');
	}

	/**
	 * write_audio
	 *
	 * @param string $data
	 * @param string $caption
	 * @param string $private
	 * @param string $tags
	 * @param string $format
	 * @param string $group
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function write_audio($data, $caption = '', $private = '', $tags = '', $format = '', $group = '')
	{
		$params = array
		(
			'type'		=> 'audio',
			'data'		=> $data,
			'caption'	=> $caption
		);

		$params += $this->extra($private, $tags, $format, $group);

		return $this->post($params)->api_call('/write');
	}

	/**
	 * extra
	 *
	 * @param string $private
	 * @param string $tags
	 * @param string $format
	 * @param string $group
	 * @return array
	 * @access protected
	 *
	 */
	protected function extra($private = '', $tags = '', $format = '', $group = '')
	{
		$this->api_id = 'www';

		$params = array();

		if ($private != '')
			$params['private'] = (int) $private;

		if ($tags != '')
			$params['tags'] = self::implode($tags);

		if ($format != '')
			$params['format'] = $format;

		if ($group != '')
		{
			if (ctype_digit( (string) $group))
			{
				$this->api_url = '.tumblr.com/group/'.$group;
			}
			else
			{
				$this->api_id = $group;
			}
		}

		return $params;
	}

	/**
	 * post
	 *
	 * @param mixed $args
	 * @return object
	 * @access protected
	 *
	 */
	protected function post($args)
	{
		$this->post = true;
		$this->post_args = array_merge(array('email' => $this->email, 'password' => $this->password), $args);

		return $this;
	}

	/**
	 * api_call
	 *
	 * @param string $api_url
	 * @return mixed
	 * @access protected
	 *
	 */
	protected function api_call($api_url)
	{
		if ( ! $result = parent::api_call($api_url, false))
			throw new Kohana_User_Exception('Tumblr Driver', Kohana::lang('service.invalid_response', $this->last_api_call(), $this->last_status_code()));

		if (ctype_digit( (string) $result))
			return $result;

		$xml = simplexml_load_string($result);

		if (false === $xml)
			throw new Kohana_User_Exception('Tumblr Driver', Kohana::lang('service.xml_parse_error'));

		return $xml;
	}

	/**
	 * api_url
	 *
	 * @param string $path
	 * @param string $separator
	 * @return string
	 * @access protected
	 *
	 */
	protected function api_url($path, $separator = '&')
	{
		return $this->api_id . $this->api_url . $path;
	}

} //End Tumblr Driver
